package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 刘付磊
 * @date 2021/10/18 0018
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishService dishService;

    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表
        this.save(dishDto);
        /*dishMapper.insert(dishDto);*/

        Long dishId = dishDto.getId();

        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();

        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishId);
        }
       /*flavors.stream().map((item)->{
            item.setDishId(dishId);
            return item;
       }).collect(Collectors.toList());*/

        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public R<IPage<Dish>> page(String name, int page, int pageSize) {
        IPage<Dish> pa = new Page(page, pageSize);

        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper();

        lqw.like(name != null, Dish::getName, name);

        lqw.orderByDesc(Dish::getUpdateTime);

        dishMapper.selectPage(pa, lqw);
        // 获取dish对象，根据分类id挨个查询对应的分类名称

        List<Dish> list = pa.getRecords();

        for (Dish dish : list) {
            LambdaQueryWrapper<Category> lqw2 = new LambdaQueryWrapper();

            lqw2.eq(Category::getId, dish.getCategoryId());

            Category category = categoryMapper.selectOne(lqw2);

            dish.setCategoryName(category.getName());
        }
        return R.success(pa);
    }

    //根据id查询菜品口味和信息
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = dishMapper.selectById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> list = dishFlavorService.list(lqw);

        dishDto.setFlavors(list);
        return dishDto;
    }

    @Override
    public void upDateWithFlavor(DishDto dishDto) {
        this.updateById(dishDto);
        //删除原本口味表里的数据
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();

        lqw.eq(DishFlavor::getDishId, dishDto.getId());

        dishFlavorService.remove(lqw);

        Long id = dishDto.getId();
        //获取口味数据
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        //添加到口味列表
        dishFlavorService.saveBatch(flavors);
    }

   /*@Override
    public R<List<Dish>> list(Dish dish) {
         LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
         lqw.eq(dish.getCategoryId() !=null ,Dish::getCategoryId,dish.getCategoryId());

         lqw.eq(Dish::getStatus,1);

         lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishes = dishMapper.selectList(lqw);
        return R.success(dishes);
    }*/

    //一定要多看这个改造代码
    @Override
    public R<List<DishDto>> list(Dish dish) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());

        lqw.eq(Dish::getStatus, 1);

        lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> dishes = dishMapper.selectList(lqw);
        List<DishDto> dishdto = dishes.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();//分类ID
            //根据ID查询分类对象
            Category category = categoryMapper.selectById(categoryId);
            if (category != null) {
                String name = category.getName();
                dishDto.setCategoryName(name);
            }
            Long id = item.getId();
            LambdaQueryWrapper<DishFlavor> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(DishFlavor::getDishId, id);
            List<DishFlavor> list = dishFlavorService.list(lqw1);
            dishDto.setFlavors(list);
            return dishDto;
        }).collect(Collectors.toList());

        return R.success(dishdto);
    }

    @Override
    public void statusBy(String ids) {
        LambdaQueryWrapper<Dish> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ids != null,Dish::getId,ids);
        Dish dish = dishMapper.selectOne(lqw);
        Integer status = dish.getStatus();
        if (status ==1){
            dish.setStatus(0);
        }else {
            dish.setStatus(1);
        }
        dishMapper.updateById(dish);
    }

    @Override
    public R<String> dtIds(Long[] ids) {
        //dishService.removeById(ids);
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(ids !=null,Dish::getId,ids);
        List<Dish> dishes = dishMapper.selectList(lqw);
        List<Long> list=new ArrayList<>();
        for (Dish dish : dishes) {
            Long id = dish.getId();
            list.add(id);
        }
        dishMapper.deleteBatchIds(list);
        return R.success("删除成功");
    }
}

